package com.ctsi.ssdc.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ctsi.ssdc.domain.LitemallCoupon;
import com.ctsi.ssdc.repository.LitemallCouponMapper;
import com.ctsi.ssdc.service.LitemallCouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.domain.Pageable;
import com.ctsi.ssdc.model.PageResult;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * Service Implementation for managing LitemallCoupon.
 *
 * @author ctsi-biyi-generator
 *
 */
@Service
public class LitemallCouponServiceImpl extends ServiceImpl<LitemallCouponMapper, LitemallCoupon> implements LitemallCouponService {

    private final Logger log = LoggerFactory.getLogger(LitemallCouponServiceImpl.class);

    @Autowired
    LitemallCouponMapper litemallCouponMapper;


    @Override
    public PageResult<LitemallCoupon> findFirstStringColumn(String name, Pageable pageable) {
        String str = name;

        Page<LitemallCoupon> litemallCouponPage = null;
        QueryWrapper<LitemallCoupon> queryWrapper = new QueryWrapper<>();

        if (Objects.nonNull(pageable)) {
            PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        }

        if (pageable != null && pageable.getSort() != null) {
            pageable.getSort().forEach(sort -> {
                if ("ASC".equalsIgnoreCase(sort.getDirection().toString())) {
                    queryWrapper.orderByAsc(sort.getProperty());
                } else {
                    queryWrapper.orderByDesc(sort.getProperty());
                }
            });
        }
        if (!StringUtils.isEmpty(str)) {
            queryWrapper.like("Name", str);
        }

        List<LitemallCoupon> data = litemallCouponMapper.selectList(queryWrapper);

        long count = 0L;

        // result count
        if (CollectionUtils.isNotEmpty(data)) {
            count = litemallCouponMapper.selectCount(queryWrapper);
        }

        return new PageResult<LitemallCoupon>(data, count, count);

    }
}